package com.aaa.tmr.controller;

import com.aaa.tmr.entity.Dictionary;
import com.aaa.tmr.service.DictionaryService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.github.pagehelper.PageInfo;
import com.aaa.tmr.util.Result;
import com.aaa.tmr.util.ReturnStatus;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * (Dictionary)表控制层
 *
 * @author makejava
 * @since 2021-03-26 10:03:25
 */
@Controller
@RequestMapping("dictionary")
public class DictionaryController {
    /**
     * 服务对象
     */
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private HttpSession session;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @RequestMapping("selectOne")
    public Dictionary selectOne(Integer id) {
        return this.dictionaryService.queryById(id);
    }

    /**
     * 跳转部门分页列表页面
     *
     * @return
     */
    @RequestMapping("toDictionary")
    public String toPage() {
        return "back/dictionary-page";
    }


    /**
     * 分页查询方法
     *
     * @param page
     * @param limit
     * @param dictionary
     * @return
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Dictionary dictionary) {
        PageInfo pageInfo = dictionaryService.queryAll(page, limit, dictionary);
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }


    /**
     * 部门添加
     *
     * @param dictionary
     * @return
     */
    @ResponseBody
    @PostMapping("insert")
    public Result insert(Dictionary dictionary) {
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, dictionaryService.insert(dictionary));
    }

    /**
     * 部门更新
     *
     * @param dictionary
     * @return
     */
    @ResponseBody
    @PutMapping("update")
    public Result update(Dictionary dictionary) {
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, dictionaryService.update(dictionary));
    }

    /**
     * 通过主键删除数据
     *
     * @param dictionaryId 主键
     * @return 是否成功
     */
    @ResponseBody
    @DeleteMapping("deleteById")
    public Result deleteById(Integer dictionaryId) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.dictionaryService.deleteById(dictionaryId));
    }


    /**
     * 通过主键删除数据
     *
     * @param dictionaryIds 主键
     * @return 是否成功
     */
    @ResponseBody
    @DeleteMapping("deleteByIds")
    public Result deleteByIds(String dictionaryIds) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.dictionaryService.batchDeleteById(dictionaryIds));
    }


}
